home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Camelot / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].zip / Camelot 078 (1990-06)(Swedish User Group of Amiga)(SE)(PD)[WB].adf / TrackSalve / TrackSalve / Tech.doc < prev    next >
Text File  |  1990-06-17  |  19KB  |  319 lines

  1.  
  2.     This text contains some background technical info.  It is not a manual,
  3.     but it fills in some data regarding the problems with disk control and
  4.     how things are implemented. (And I love to talk about it after all this
  5.     work)
  6.  
  7.  
  8.                                INTRODUCTION
  9.  
  10.     The rotational speed of an Amiga disk is 300 rpm.  The time of one
  11.     revolution is .2 seconds.  Data is sampled at .5 MHz.  So a track on an
  12.     Amiga floppy disk contains 100000 bits.  This can vary a little due to
  13.     motor speed variations.  Alas we cannot put any desired pattern in these
  14.     bits.  Therefore data is converted to patterns which are allowed.  This
  15.     conversion, called MFM-encoding, has an efficiency of 50 percent.  For
  16.     every bit of data two bits of the magnetic track are used.  We can
  17.     picture an encoded sector as follows:
  18.  
  19.        4 bytes inter sector gap
  20.        4 bytes sync pattern
  21.        8 bytes sector ID
  22.       32 bytes label 
  23.        8 bytes checksum over sector ID and label
  24.        8 bytes checksum over data block
  25.     1024 bytes data block
  26.  
  27.     Therefore the size of a sector is 1088*8=8704 bits. 100000/8704=11,
  28.     remainder 4256. Thus we can store 11 sectors.  We have 4256 bits left we
  29.     donot use for storage.  Instead these bits allow us to accept a motor
  30.     speed increase of 4.25 %. If we allow the same speed decrease, we must
  31.     be able to supply a non used space of roughly 8500 bits or 1100 bytes.
  32.     In fact, Trackdisk is prepared for an unused space of 1660 bytes. (Why
  33.     that much?) 
  34.  
  35.     Trackdisk does not write separate sectors as found on other systems.
  36.     Instead it writes out a complete track, actually reformatting the track.
  37.     That is why blocks can be so tight together. (On the other systems you
  38.     need gaps between the sectors and between sector ID and data block to
  39.     allow soft and hardware to synchronize) If Trackdisk writes out a track,
  40.     it begins with the 1660 bytes gap, followed by the sectors.  Although
  41.     the sectors are written out in order, the first sector can be any of the
  42.     eleven.  The last sector is written over the begin of the gap, thus
  43.     effectively removing old data and patterns.  Although Trackdisk allows
  44.     for synchronisation with the disk index, this is normaly not used;
  45.     Trackdisk just starts writing when it is ready to do so.
  46.  
  47.     Reading is something else.  If Trackdisk should wait for the gap to
  48.     pass, average halve a tracktime is done nothing.  Therefore reading is
  49.     started, ignoring where the head is in the track.  To ensure that all
  50.     sectors are read, the readsize must be one sector larger than the number
  51.     of available sectors.  And the gap must be read too.  This results in a
  52.     readsize of 12*1088+1660=14716 bytes or 117728 bits, 18% more than a
  53.     physical track.  Usually the read is started somewhere within a sector,
  54.     continues until the gap, the gap itself and enough sectors after the gap
  55.     to cover the whole track.  Reading and writing is done by DMA (Direct
  56.     Memory Access) and word wide (16 bits). Therefore data read from disk
  57.     can be in memory shifted up to 15 bits.  The part behind the gap has no
  58.     exact relation in position and shift to the first read part.  To recover
  59.     the track, a search is started for a sync pattern.  If found, this
  60.     informs us about the shift.  Knowing the shift, the following sector id
  61.     can be decoded.  This sector id has the following form and are all
  62.     bytes:
  63.  
  64.     $ff (formatbyte)
  65.     tracknumber
  66.     sectornumber
  67.     number of sectors until gap
  68.  
  69.     The last item "number of sectors until gap" tells us how large the part
  70.     is with the current shift.  This part is shifted and moved to the begin
  71.     of the data part of the trackbuffer.  Then a second search for a
  72.     syncword is done, starting behind the the first part.  The remaining
  73.     sectors are found and moved and shifted tight behind the first part.
  74.     Now there is in the buffer a block of 11 sectors and after updating the
  75.     last items of the sector id's, "number of sectors until gap", the track
  76.     is ready to be written again.
  77.     But before that a lot can go wrong.  If some syncword or intersector gap
  78.     is distorted, or one of the 22 checksums is wrong, Trackdisk stops and
  79.     returns an error.  If the error concerns a syncword or a gap, or if a
  80.     faulting checksum belongs to a sector ID, it is likely that all data can
  81.     be recovered (exept for the label, but that is not used by Dos). And if
  82.     the checksum belongs to a data block, but this data block is not
  83.     requested, then the other blocks could be read without a problem.  Of
  84.     course a track with an error must not be written back to disk.
  85.  
  86.  
  87.                               SALVE FUNCTION
  88.  
  89.     The salve function of TrackSalve reads data from disk in such a large
  90.     buffer, that there will be an uninterrupted array of sectors in this
  91.     buffer.  If one sector can be found with a good checksum over its
  92.     header, all data to calculate the position of the other sectors is
  93.     available.  The data and label blocks are copied to the original buffer,
  94.     this time with sector 0 in the first position.  If the checksum of the
  95.     current block is correct, a corresponding bit is cleared in a word that
  96.     is initiated with 11 ones.  Such a word exists as well for the data as
  97.     the label blocks.  There is a byte that contains normally the number of
  98.     the first sector, but it will contain an error number if the track is
  99.     bad.  The Trackdisk errors start at 20, so if the byte is larger than
  100.     10, the track is bad.  Somewhere within trackdisk this is tested and as
  101.     result the tracknumber is set to -1, meaning "trackbuffer not valid".
  102.     Therefore any subsequent read request will result in a seek and several
  103.     read retries of the track.  If the salve function is active, this
  104.     invalidation of the tracknumber is prevented.  Instead the first sector
  105.     byte is tested at a read request and if this indicates an error, the
  106.     bitpattern with bad blocks are tested.  As soon as the request concerns
  107.     a bad block, the error value and the number of transferred bytes is set
  108.     and fixed, but transfers continues until the number of requested bytes
  109.     are copied.  There is no difference between the originial behaviour of
  110.     trackdisk and the salve function other than the bonus of the extra
  111.     transferred data (IO_ERROR contains the error and IO_ACTUAL contains the
  112.     number of bytes without error). If label transfers is switched on,
  113.     IO_ACTUAL presents the number of sectorbytes transferred until an error
  114.     is encountered in a label OR sector.  Some specialised recovery program
  115.     can use this info to recover more data at the cost of accuracy, which
  116.     may be acceptable in some cases.  A write to the same track still will
  117.     set this tracknumber to -1, and the normal retry scheme will be used.
  118.     The salve function is rather introvert and will at a priority of +5
  119.     disturb the feeling of the Amiga.  So the priority is lowered to -2
  120.     during track salvage.
  121.  
  122.  
  123.                                 MFM FLAW
  124.  
  125.     How heavy the tests are on the track read from disk, there is a serious
  126.     gap (to my opinion) in these itegrity checks.  As stated earlier, not
  127.     every pattern is allowed in the magnetic track.  A MFM encoded data
  128.     block exist of true data bits and timing bits.  The databits are always
  129.     valid:  these are not coded, but the timing bits, these are to be found
  130.     between the data bits, are meaningless as data.  They are just there to
  131.     prevent too much and to less subsequent magnetic transitions.  Too much
  132.     is beyond the bandwith of the medium and too less may lead to loss of
  133.     synchronisation and gives noise a chance.  The used recording technique
  134.     is a transition on a one and no transition on a zero.  MFM coding
  135.     defines patterns where a one always is followed by a zero, and there are
  136.     never more than three subsequent zeros.  What happens if there are two
  137.     subsequent ones?  Or more than three zeros?  The system is not that
  138.     sensitive that this immediately will lead to bad reads of the data
  139.     containing bits.  And Trackdisk maintains checksums over encoded data,
  140.     or..? But ONLY over the data bits, NOT over the timing bits!  If a
  141.     timing bit is read wrong from disk, this will not be detected.  The
  142.     sector is copied to a new position in the trackbuffer, and if an other
  143.     sector is changed, it will be written back to disk with the faulting
  144.     timing bit!  If the sector is not updated (eg belonging to a program)
  145.     this faulting bit wil stay forever.  Slowly all timing bits can
  146.     disappear due to the retry scheme of trackdisk.  At a moment the track
  147.     will be unreadable.  Especially the roottrack is sensitive for this
  148.     phenomen.
  149.     TrackSalve has modied Trackdisk in such way that just before a track is
  150.     written to disk, all timing bits are set according to the MFM standard.
  151.     This takes very little time because it is all done by the blitter.  Now
  152.     that it is ensured that the track is MFM updated before it goes to disk,
  153.     the encoding routines can ignore these bits, and that saves cpu time.
  154.     Because the encoding routines were rewritten, it was easy to allow non
  155.     chip memory for IO buffers.  Non-chip buffers are copied by the cpu at
  156.     the most efficient way (cribbed from exec's CopyMem()), although
  157.     CopyMem() is used if it is replaced.
  158.  
  159.  
  160.                              BUGS IN TRACKDISK
  161.  
  162.     I knew of two bugs and found a third.  These all are repaired in
  163.     TrackSalve.
  164.  
  165.     Raw read and write had problems because IO_LENGTH was compared with
  166.     $8000 instead of #$8000. The difference is that IO_LENGTH should be
  167.     compared with the value 8000 hex.  Instead it was compared with some
  168.     value that was at memory location 8000 hex.
  169.  
  170.     In a multitasking environt the hardware must be shared between the
  171.     concurrent tasks.  There can be up to four drives connected to the
  172.     Amiga.  For each drive a Trackdisk task is started (all executing the
  173.     same code but with separate data area's). These four drives are
  174.     controlled by one piece of hardware (some custom chip I forgot the name
  175.     but I know its address). The TD-tasks use it to control their particlar
  176.     drives.  The tasks ask permission to the disk.resource to access this
  177.     chip directly by a call named GetUnit(). If the task is ready with it,
  178.     this is told to the disk.resource by a call named GiveUnit(). Another
  179.     TD-task that perhaps was waiting for it, gets access permission.  There
  180.     is a nasty bug in Trackdisk which could disturb the permission scheme.
  181.     Because a branch was too far, a call to GetUnit() was missed if that
  182.     drive was empty.  The task asumed permission, did some access and called
  183.     GiveUnit(). This is not as bad as it looks, because the time of violated
  184.     hardware access is very short, and was very short after a permitted
  185.     access.  But if another TD-task had got permission (and the more drives
  186.     the more likely this will happen), the turnips are coocked.  The other
  187.     tasks commands to the hardware are possibly destroyed and to make
  188.     matters worse, it looses its permission to access by the first tasks
  189.     call to GiveUnit(). So now this ignorent task is violating the rules.
  190.     The patch done by the setpatch program modifies disk.resource in such
  191.     way that only GiveUnits() are valid from tasks that did the GetUnit().
  192.     The access violation was not repaired.
  193.  
  194.     During the tests with special Trackdisk IO programs, some strange
  195.     behaviour of Trackdisk came up.  Dos is very precise in its IO with
  196.     Trackdisk, but a Trackdisk tester is not.  That must explain why this
  197.     was not disturbing more general.  If a write to a protected disk was
  198.     done, this disk became unreadable for all other tracks than that was
  199.     just written to.  Reads via Dos resulted in a Read/Write error requester
  200.     although retry selection solved the problem (CMD_CLEAR). A new bug in
  201.     Trackdisk?  Yes!  If you write a sector to a protected disk, first the
  202.     track is read into the trackbuffer, followed by a copy of the sector
  203.     into this buffer, replacing the original, and a flag is set, meaning
  204.     "trackbuffer changed". If you want to read a sector from another track,
  205.     trackdisk tries to flush (write to disk) its trackbuffer.  The disk is
  206.     protected and the attempt will fail.  So you cannot read another track!
  207.     But it is even worse if you want to read from the track currently in the
  208.     buffer.  You think the data is coming from disk, but it might be the
  209.     never updated sector!  What is the problem?  Trackdisk does not check
  210.     the protect status before letting a CMD_WRITE modify the trackbuffer.
  211.  
  212.  
  213.                           AUTO UPDATE FUNCTION
  214.  
  215.     There were times when I used Sectorama (I still use this nice program
  216.     from the hand of David Joiner), I did not understand why sometimes a
  217.     modified and written sector would not stick on disk.  Now I know this is
  218.     because Trackdisk needs some help.  It must be told to flush its
  219.     buffers, although this is automaticly done if some data is requested
  220.     from another track.  Trackdisk also does not switch off its drive motor.
  221.     If not be told to switch it off, the motor runs forever.  Dos knows
  222.     about Td's lack of updating and does some special commands:  CMD_UPDATE
  223.     and TD_MOTOR.  Dos does this well within five seconds.  But programs
  224.     that directly access Td, may not be aware and leave the motor running
  225.     and the buffers unflushed.
  226.     There are a lot of timers in Trackdisk, so why does not Td do it by
  227.     it self?  I do not know, but if you have the function switched on,
  228.     Td flushes a modified trackbuffer and switch the motor off after
  229.     five seconds idling.
  230.  
  231.  
  232.                             NOCLICK FUNCTION
  233.  
  234.     The presence of a disk in a drive is reflected by a line called CHNG.
  235.     This line is asserted if there is no disk in the drive.  It stays that
  236.     way until a disk in the drive and the head is stepped (and the drive is
  237.     selected of course). I asume this is done this way to give the
  238.     opportunity to acknowledge an disk extraction.  If the line directly
  239.     reflects the disk presence state, an extraction can be missed.  Well, it
  240.     is a way, I know some other, and I know too that the sound of an empty
  241.     drive generally is experienced to be annoying.  The step direction is
  242.     each time reversed, so the head will stay about the same position.  This
  243.     reverse is done by a CPU instruction "bchg", bitchange.  Therefore is
  244.     was very simple to implement the noclick function, just change the
  245.     instruction into "bset", bit set.  Then the head steps to the outside of
  246.     the disk and once arrived, it stops because it is blocked there.  This
  247.     can be by a pillow or by an electrical switch, the latter effectively
  248.     preventing stepping.  When Kickstart got in rom, this was not that easy
  249.     anymore.  TrackSalve executes Trackdisk in ram, and the patch is simple
  250.     again.  But should you have noclick on every drive?  Some drives still
  251.     make noise, this time not as regular as well in time as in sound.  Even
  252.     more disturbing!  Apart from the fact that I believe that these drives
  253.     are not suitable for this method.
  254.  
  255.  
  256.                              VERIFY FUNCTION
  257.  
  258.     A drawback of this whole track read/write system is that a simple verify
  259.     is not possible.  It is not enough to verify the just written sector,
  260.     because ten more sectors are written to disk.  These sectors have
  261.     probably no connection with the writing program, and a more general
  262.     warning must be made.  It is the particular Trackdisk task that does a
  263.     request and the user must reply on it.  The AutoRequest() has been
  264.     proven unreliable in low memory situations.  It returns "Cancel" if the
  265.     function could not be carried out, where it should return "Retry". The
  266.     same is true for an Alert(). Therefore TrackSalve build its own
  267.     requester.  
  268.     My mouse is often hard to find, and if found, I have to search for a
  269.     surface for it (the screen would be nice but it's too steep). So I like
  270.     keyboard shortcuts.  This is well implemented in the system requester.
  271.     If you build your own requester, suddenly all input is blocked, and I
  272.     found no way (other than an special inputhandler, which is beyond I am
  273.     willing to implement in a patch) to receive shortcuts.  What is a
  274.     requester other than a window with some gadgets?  Then I can receive
  275.     everything I like.  But this time Dmouse does not recognize the
  276.     requester as a requester.  Sigh.    It doesn't get any easier with
  277.     Intuition.  
  278.     Do do the verify, the track is read back into another buffer and then
  279.     compared with the original.  This is broken in two parts, because the
  280.     data in the verify buffer probably begins with another sector.  The
  281.     comparision is done by the blitter, xor-ing both buffers.
  282.     The motor is switched off before the requester is displayed.  The
  283.     writing program however is not notified and may not switch off a motor
  284.     of a source.  The standard diskcopy is a good example.  If you use
  285.     diskcopy without its verify function, but with TrackSalve verifying
  286.     (which is faster), and a verify error happens, the destination drive
  287.     will stop turning.  Diskcopy does not know and does not switch off its
  288.     source drive.  This is a good reason to enable the auto update function
  289.     in TrackSalve, which will stop the source's motor.
  290.  
  291.  
  292.                                    BUGS?
  293.  
  294.     The preceding release of TrackSalve (1.0) had a bug in it, although it
  295.     was extremely unlikely that the buggy code would be executed.  This
  296.     unlikelyness was the reason that the bug slipped through the the
  297.     debugging phase.  It brought me to an improved debugging administration
  298.     and every single branch is executed under debug control, how trivial a
  299.     branch might look.  Pieces of code that could behave unexpectable
  300.     because of special combinations of parameters, have been run under
  301.     simulators producing all possible combinations.  This time I would like
  302.     to qualify the debugging as "extreme". Again:  This program is extremely
  303.     debugged!
  304.  
  305.  
  306.                                 CONCLUSION
  307.  
  308.     With this patch Trackdisk becomes more efficient in its CPU use,
  309.     although slightly slower (well under 2%) in its writing, because of the
  310.     MFM update.  A lot features are added, which are fully transparant.
  311.     Trackdisk has come closer to what I think a floppy disk device driver
  312.     should be.  Better requireres a complete rewrite.  (Sorry Commodore,
  313.     but disk drivers are my favorite pastime, yours too?).
  314.  
  315.     D.W.Reisig
  316.     Woudweeren 10
  317.     1151 AV  Broek in Waterland
  318.     Holland
  319.